package cn.cq.admin.service.impl;

import cn.cq.admin.entity.model.ScoreUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cq.admin.entity.model.UserCourseCourseware;
import cn.cq.admin.service.UserCourseCoursewareService;
import cn.cq.admin.mapper.UserCourseCoursewareMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
* @author  dicraft
* @description 针对表【user_course_courseware】的数据库操作Service实现
*/
@Service
public class UserCourseCoursewareServiceImpl extends ServiceImpl<UserCourseCoursewareMapper, UserCourseCourseware>
    implements UserCourseCoursewareService{

    @Override
    public void doModify(UserCourseCourseware ucc) {
        lambdaUpdate().eq(UserCourseCourseware::getUserId, StpUtil.getLoginIdAsLong())
                .eq(UserCourseCourseware::getCourseId, ucc.getCourseId())
                .eq(UserCourseCourseware::getCoursewareId, ucc.getCoursewareId())
                .set(ObjectUtil.isNotNull(ucc.getEnd()),UserCourseCourseware::getEnd, ucc.getEnd())
                .set(UserCourseCourseware::getNotes, ucc.getNotes())
                .update();
    }

    @Override
    public Collection<ScoreUtil>  getScore() {
        long userId = StpUtil.getLoginIdAsLong();
        //通过userId获取所有成绩

        List<UserCourseCourseware> uccs
                = lambdaQuery()
                .eq(UserCourseCourseware::getUserId, userId)
//                .groupBy(UserCourseCourseware::getCoursewareId)
                .list();
        //获取所有课程
        HashMap<Long, ScoreUtil> map = new HashMap<>();
        uccs.stream().forEach(ucc->{
            if (map.containsKey(ucc.getCourseId())) {
                ScoreUtil scoreUtil = map.get(ucc.getCourseId());
                scoreUtil.setCount(scoreUtil.getCount()+1);
                if (ucc.getEnd().equals(1)) {
                    scoreUtil.setEnd(scoreUtil.getEnd()+1);
                }
                Double doubleScore = scoreUtil.getEnd() / Double.valueOf(scoreUtil.getCount());
                Double v1 = doubleScore * 100;
                scoreUtil.setScore(v1.longValue()/(double)100 * 100);
            }else {

                ScoreUtil scoreUtil = new ScoreUtil();
                scoreUtil.setCount(1);
                if (ucc.getEnd().equals(0)){
                    scoreUtil.setEnd(0);
                    scoreUtil.setScore(0.0);
                }else{
                    scoreUtil.setEnd(1);
                    scoreUtil.setScore(100.0);
                }
                scoreUtil.setCourseId(String.valueOf(ucc.getCourseId()));
                map.put(ucc.getCourseId(), scoreUtil);
            }
        });
        Collection<ScoreUtil> values = map.values();
        return values;
    }

    @Override
    public String getNotes(Long courseId, Long coursewareId) {
        long userId = StpUtil.getLoginIdAsLong();
        UserCourseCourseware one = lambdaQuery().eq(UserCourseCourseware::getUserId, userId)
                .eq(UserCourseCourseware::getCourseId, courseId)
                .eq(UserCourseCourseware::getCoursewareId, coursewareId)
                .one();
        return one==null? null:one.getNotes();
    }
}




